str = new String( "The Gingham Dog" );
The new
operator creates an object using the constructor String.
Review: Two steps take place when an assignment operator is executed:
These steps happen like this:
String str; // name for a future object
str = new String( "The Gingham Dog" );
--+-- ----------------+--------------
| |
|
| 1. An object is created using the constructor.
| The Java system keeps track of
| how to find the object.
2. The way to find the object is stored in the variable str
.
An object reference is information on how to find a particular object.
The object is a chunk of main memory;
a reference to the object is a way to get to that chunk of memory.
The variable str
does not actually contain the object,
but contains information about where the object is.
Objects are created while a program is running. Each object has a unique object reference, which is used to find it. When an object reference is assigned to a variable, then that variable says how to find that object.
In diagrams, an object reference is usually shown as an arrow from the object reference variable to the object.